package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.exception.BusinessException;
import com.workManager.mapper.TbPaymentPlanMapper;
import com.workManager.mapper.TbProjectMapper;
import com.workManager.mapper.TbProjectPaymentMapper;
import com.workManager.pojo.*;
import com.workManager.util.DateUtil;
import com.workManager.util.WebUtil;
import com.workManager.vo.PaymentPlanVo;
import com.workManager.vo.ProjectPaymentVo;
import com.workManager.vo.ProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Thinkpad on 2017/1/16 0016.
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private TbProjectMapper projectMapper;

    @Autowired
    private TbProjectPaymentMapper projectPaymentMapper;

    @Autowired
    private TbPaymentPlanMapper paymentPlanMapper;


    public TbProject selectByProjectName(String projectName) {
        return projectMapper.selectByProjectName(projectName);
    }

    public TbProject selectByProjectCode(String projectCode) {
        return  projectMapper.selectByProjectCode(projectCode);
    }

    public int add(TbProject project) throws DuplicateKeyException { return projectMapper.insertSelective(project);}

    public int deleteByProjectIds(String[] projectIds){return projectMapper.deleteByProjectIds(projectIds);}

    public int update(TbProject project) {return projectMapper.updateByPrimaryKey(project);}

    public PageInfo<ProjectVo> query(Map<String, Object> map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        List<ProjectVo> projectList = projectMapper.query(map);
        PageInfo<ProjectVo> projectPageInfo = new PageInfo<ProjectVo>(projectList);
        return  projectPageInfo;
    }

    public PageInfo<TbProject> queryByProjectStatus(Map<String, Object> map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        List<TbProject> projectList = projectMapper.queryByProjectStatus(map);
        PageInfo<TbProject> projectPageInfo = new PageInfo<TbProject>(projectList);
        return projectPageInfo;
    }

    public int insertPayment(TbProjectPayment projectPayment) {
        return projectPaymentMapper.insert(projectPayment);
    }

    public int updatePayment(TbProjectPayment projectPayment) {
        return projectPaymentMapper.updateByPrimaryKey(projectPayment);
    }

    public int deletePayment(TbProjectPayment projectPayment) {
        return projectPaymentMapper.deleteByPrimaryKey(projectPayment.getPaymentId());
    }

    public int delete(List<Integer> idList) {
        for(Integer id:idList){
            TbProjectPayment projectPayment = projectPaymentMapper.selectByPrimaryKey(id);
            projectPaymentMapper.deleteByPrimaryKey(projectPayment.getPaymentId());
            TbPaymentPlanExample example = new TbPaymentPlanExample();
            example.createCriteria().andPaymentIdEqualTo(projectPayment.getPaymentId());
            List<TbPaymentPlan> list = paymentPlanMapper.selectByExample(example);
            if(list.size()>0){
                TbPaymentPlan paymentPlan = list.get(0);
                Map map = new HashMap();
                map.put("remark", paymentPlan.getRemark()+"|["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:已删除实际收款，报废");
                map.put("id", paymentPlan.getId());
                paymentPlanMapper.invalid(map);
            }
        }
        return idList.size();
    }

    public int updatePaymentRatio(List<Integer> idList, String ratio) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids",idList);
        map.put("allocationRatio",ratio);
        return projectPaymentMapper.updatePaymentRatio(map);
    }

    public PageInfo<ProjectPaymentVo> queryPayment(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<ProjectPaymentVo> paymentList = projectPaymentMapper.queryPayment(map);
        PageInfo<ProjectPaymentVo> paymentPageInfo = new PageInfo<ProjectPaymentVo>(paymentList);
        return  paymentPageInfo;
    }

    public TbProjectPayment queryPaymentById(Integer id) {
        return projectPaymentMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取所有有效项目
     * @return
     */
    public List<TbProject> getAllProjects(){
        //获取参数类型ID
        TbProjectExample projectExample = new TbProjectExample();
        projectExample.or().andIsValidEqualTo(true);
        return projectMapper.selectByExample(projectExample);
    }

    /**
     * 根据项目列表获取“项目编号-名称”与“项目ID”的键值对
     * @param projectList
     * @return
     */
    public Map<String, Integer> getNameAndIdMapByProjects(List<TbProject> projectList){
        Map<String, Integer> map = new HashMap<String, Integer>();

        Iterator<TbProject> iterator = projectList.iterator();
        while (iterator.hasNext()){
            TbProject project = iterator.next();
            map.put(project.getProjectCode() + "-" + project.getProjectName(), project.getProjectId());
        }

        return map;
    }

    /**
     * 根据项目列表获取“项目编号”与“项目ID”的键值对
     * @param projectList
     * @return
     */
    public Map<String, Integer> getCodeAndIdMapByProjects(List<TbProject> projectList){
        Map<String, Integer> map = new HashMap<String, Integer>();

        Iterator<TbProject> iterator = projectList.iterator();
        while (iterator.hasNext()){
            TbProject project = iterator.next();
            map.put(project.getProjectCode(), project.getProjectId());
        }

        return map;
    }

    /**
     * 根据项目列表获取“项目编号”与“项目ID”的键值对
     * @param projectList
     * @return
     */
    public List<String> getProjectCodeList(List<TbProject> projectList){
        List<String> list = new ArrayList<String>();

        Iterator<TbProject> iterator = projectList.iterator();
        while (iterator.hasNext()){
            TbProject project = iterator.next();
            list.add(project.getProjectCode());
        }

        return list;
    }

    @Override
    public ProjectVo queryByCodeName(Map<String, Object> map) {
        return projectMapper.queryByCodeName(map);
    }

}
